

#include "at_cmd_analyse.h"
#include "at_cmd_callback.h"
#include "ringbuff.h"


int16_t at_cmd_analyse(STR_RINGBUF *reci_buf);
static char * STRTOK (char *s, const char *delim);
uint16_t search_param_in_str(char *in, char **out,uint16_t max_num);
static int find_ATCmd(char* p8Param, ATList_st* stATCmdList,uint16_t n);
void fac_at_parse(char* p8Param, uint16_t len);

/*!
    \brief      at_cmd_analyse
    \param[in]  none
    \param[out] none
    \retval     none
*/

int16_t at_cmd_analyse(STR_RINGBUF *reci_buf)
{

    uint16_t cmd_cnt;
    char reci_temp[50] = {0};
    char reci_data = 0;

    if(ringBufRead(reci_buf, &reci_data, 1))
    {
        if(reci_data == 'A')
        {
            reci_temp[0] = reci_data;
            for(cmd_cnt=1;cmd_cnt<50;cmd_cnt++)
            {
                if(ringBufRead(reci_buf, &reci_data, 1))
                {
                    reci_temp[cmd_cnt] = reci_data;

                    if(reci_data == '%')
                    {
                        break;
                    }
                }

                else
                {
                    return 0;
                }
            }

            fac_at_parse(reci_temp, cmd_cnt+1);

            return 1;
        }

        return 0;
    }

    else
    {
        return 0;
    }
}

/*!
    \brief      STRTOK
    \param[in]  none
    \param[out] none
    \retval     none
*/
static char * STRTOK (char *s, const char *delim)
{
    static char *olds;
    char *token;

    if (s == NULL)
    {
        s = olds;
    }

    /* Scan leading delimiters.  */
    s += strspn (s, delim);
    if (*s == '\0')
    {
        olds = s;
        return NULL;
    }

    /* Find the end of the token. */
    token = s;
    s = strpbrk (token, delim);
    if (s == NULL)
    {
        /* This token finishes the string.  */
        olds = s;
    }
    else
    {
        /* Terminate the token and make OLDS point past it.  */
        *s = '\0';
        olds = s + 1;
    }
    return token;
}

/*!
    \brief      search param in string
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint16_t search_param_in_str(char *in, char **out,uint16_t max_num)
{
    uint16_t ret = 0;

    out[ret] = STRTOK(in,AT_CMD_BREAK);
    while( out[ret] != NULL ) {
      ret++;
      if(ret >= max_num)
          break;
      out[ret] = STRTOK(NULL, AT_CMD_BREAK);
   }
   return ret;
}


/*!
    \brief      find AT cmd
    \param[in]  none
    \param[out] none
    \retval     none
*/
static int find_ATCmd(char* p8Param, ATList_st* stATCmdList,uint16_t n)
{
    int s32Pos;
    int i;

    for(i = 0; i < n; i++)
    {
        if(stATCmdList[i].ps8ATcmd!=NULL)
        {
            s32Pos = strlen(stATCmdList[i].ps8ATcmd);
            if(0 == memcmp(p8Param, stATCmdList[i].ps8ATcmd, s32Pos))
            {
                if (NULL != stATCmdList[i].pfnHandle)
                {
                    stATCmdList[i].pfnHandle(&p8Param[s32Pos]);
                    return 0;
                }
            }
        }
    }
    return -1;
}




/*!
    \brief      fac_at_parse
    \param[in]  none
    \param[out] none
    \retval     none
*/

void fac_at_parse(char* p8Param, uint16_t len)
{
    int s32Ret = -1;

    if(p8Param[0] == *AT_CMD_HEAD0 && p8Param[1] == *AT_CMD_HEAD1 && p8Param[len-1] == *AT_CMD_TAIL)
    {
        p8Param[len-1] = '\0';
        s32Ret = find_ATCmd(p8Param,stATCmdList,sizeof(stATCmdList)/sizeof(ATList_st));

        if(s32Ret == -1)
        {
            UART_PrintfMesg("AT NOT EXIST!;\n");
        }
    }
    else
    {
        UART_PrintfMesg("AT HEAD or TAIL ERR!;\n");
    }
}

